<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Family;
use App\Models\Area;
use App\Models\FamilyPregn;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FamilyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Family(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('name');
            $grid->column('idcard_type')->using(\App\Models\Family::$idCardTypeTxt);
            $grid->column('idcard');
            $grid->column('type')->using(\App\Models\Family::$typeTxt);
            $grid->column('birthday');
            $grid->column('gender')->using(\App\Models\Family::$gender);
            $grid->column('relation');
            $grid->column('province')->using(Area::query()->pluck('title','id')->toArray());
            $grid->column('county')->using(Area::query()->pluck('title','id')->toArray());
            $grid->column('city')->using(Area::query()->pluck('title','id')->toArray());
            $grid->column('address');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();

                // append一个操作
                $actions->append('<a href="sign?family_id='.$id.'">查看签约记录</a>');

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
                $filter->equal('name');
                $filter->equal('type')->select(\App\Models\Family::$typeTxt);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Family(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('idcard_type');
            $show->field('idcard');
            $show->field('type');
            $show->field('birthday');
            $show->field('phone');
            $show->field('gender');
            $show->field('relation');
            $show->field('province');
            $show->field('county');
            $show->field('city');
            $show->field('address');
            $show->field('avatar');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Family(['childOne','pregn']), function (Form $form) {

            $form->tab('基本信息', function (Form $form) {

                $form->display('id');
                $form->text('user_id');
                $form->text('name');
                $form->radio('idcard_type')->options(\App\Models\Family::$idCardTypeTxt);
                $form->text('idcard');
                $form->date('birthday');
                $form->text('phone');
                $form->radio('gender')->options(\App\Models\Family::$gender);
                $form->text('relation');
                $form->select('province')->options(Area::query()->where('parent_id',0)->pluck('title','id'))->load('city', '/api/city');
                $form->select('city')->load('county', '/api/city');
                $form->select('county');
                $form->text('address');
                $form->image('avatar');

                $form->display('created_at');
                $form->display('updated_at');
            })->tab('其他信息', function (Form $form) {
                $form->radio('type')
                    ->options(\App\Models\Family::$typeTxt)
                    ->when(1,function (Form $form){
                        $form->text('childOne.mother','母亲姓名');
                        $form->date('childOne.mother_birthday','母亲生日');
                        $form->radio('childOne.mother_idcard_type','母亲证件类型')->options(\App\Models\Family::$idCardTypeTxt);
                        $form->text('childOne.mother_idcard','母亲证件号');
                        $form->text('childOne.birth_code','儿童出生医学编码');
                    })->when(2,function (Form $form){
                        $form->date('pregn.last_menses','末次月经时间');
                        $form->radio('pregn.domicile_type','户籍类型')->options(FamilyPregn::$domicileTypeTxt);
                        $form->text('pregn.domicile','户籍地（省）');
                        $form->text('pregn.husband_domicile','丈夫户籍地（省）');
                        $form->radio('pregn.status','妊娠状态')->options(FamilyPregn::$stateTxt);
                    });
            });





        });
    }
}
